package com.face.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.face.common.pojo.DataGridResult;
import com.face.common.pojo.FaceResult;
import com.face.common.utils.ExceptionUtil;
import com.face.mapper.TbCollegeMapper;
import com.face.pojo.TbCollege;
import com.face.pojo.TbCollegeExample;
import com.face.pojo.TbCollegeExample.Criteria;
import com.face.service.CollegeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CollegeServiceImpl implements CollegeService{
	@Autowired
	private TbCollegeMapper collegeMapper;
	
	@Override
	public DataGridResult findCollegesByPage(Integer page,Integer rows) {
		TbCollegeExample example=new TbCollegeExample();
		//分页
		PageHelper.startPage(page, rows);
		
		List<TbCollege> list = collegeMapper.selectByExample(example);
		if(list!=null&&list.size()>0){
			DataGridResult result=new DataGridResult();
			
			PageInfo<TbCollege> info=new PageInfo<>(list);
			
			result.setTotal(info.getTotal());
			result.setRows(list);
			return result;
		}
		return null;
	}

	@Override
	public FaceResult deleteByIds(Integer[] ids) {
		try {
			for(Integer id:ids){
				collegeMapper.deleteByPrimaryKey(id);
			}	
			return FaceResult.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return FaceResult.build(500,ExceptionUtil.getStackTrace(e));
		}
	}

	@Override
	public FaceResult add(TbCollege college) {
		try {
			college.setCreateTime(new Date());
			college.setUpdateTime(new Date());
			boolean b = this.findCollegeByName(college.getCollegeName());
			if(b){
				collegeMapper.insert(college);
				return FaceResult.ok();
			}
			return FaceResult.build(500,"此院系已存在");
		} catch (Exception e) {
			e.printStackTrace();
			return FaceResult.build(500, "添加院系失败，请联系相关人员!");
		}
	}

	@Override
	public boolean findCollegeByName(String name) {
		TbCollegeExample example=new TbCollegeExample();
		Criteria criteria = example.createCriteria();
		criteria.andCollegeNameEqualTo(name);
		List<TbCollege> list = collegeMapper.selectByExample(example);
		if(list!=null&&list.size()>0){
			return false;
		}
		return true;
	}

	@Override
	public FaceResult update(TbCollege college) {
		try {
			college.setUpdateTime(new Date());
			collegeMapper.updateByPrimaryKey(college);
			return FaceResult.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return FaceResult.build(500, "修改失败，联系相关人员！");
		}
		
	}

	@Override
	public List<TbCollege> findAll() {
		TbCollegeExample example=new TbCollegeExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIsNotNull();
		List<TbCollege> list = collegeMapper.selectByExample(example);
		
		return list;
	}

	@Override
	public TbCollege findById(Integer id) {
		try {
			TbCollege college = collegeMapper.selectByPrimaryKey(id);
			return college;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
